<?php

namespace common\models\datas;

use common\components\extenders\MenuManage;
use common\components\helpers\Fun;
use Yii;
use yii\helpers\ArrayHelper;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use zhtfrontend\models\datas\FullActivities;

/**
 * This is the model class for table "{{%account_record}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property string $cash
 * @property integer $site_id
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $state
 * @property string $pay_cash
 * @property string $pay_pos
 * @property string $pay_card
 * @property string $pay_scholarship
 * @property integer $free
 * @property integer $order_id
 * @property string $note
 *
 */
class AccountRecord extends \common\components\bases\BaseModel
{
    public $pay_password;
    public $p_cash;
    public $money;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%account_record}}';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['customer_id', 'cash'], 'required', 'on' => 'create'],
            [['cash', 'pay_card', 'pay_cash', 'pay_pos'], 'compare', 'compareValue' => 0, 'operator' => '>=', 'message' => '金额必须是大于0的数字'],
            [['customer_id', 'site_id', 'user_id', 'created_at', 'order_id','scholarship_rel_id','state','full_activities_id','student_id'], 'integer'],
            [['cash', 'pay_cash', 'pay_pos', 'pay_card', 'pay_scholarship','deposit','free','pay_alipay','pay_wxpay','p_cash','money'], 'number'],
            [['note'], 'string'],
            ['pay_cash','cash_check'],
            ['pay_pos','pos_check'],
            ['free','free_check'],
            [['pay_cash','pay_pos'],'actual_back_check','on'=>['cash_back']],
            ['free','charge_required','on'=>['charge']],
            ['note','required','on'=>['charge']],
            ['pay_card','validate_cash','skipOnEmpty'=>false,'on'=>['orders']],
            ['pay_password','validate_password','skipOnEmpty'=>false,'on'=>['orders','payproduct']],
            ['free','charge_activities','on'=>['charge']],
            ['pay_card','validate_money','skipOnEmpty'=>false,'on'=>['payproduct']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'customer_id' => Yii::t('common', '会员编号'),
            'cash' => Yii::t('common', '实际支付总金额'),
            'site_id' => Yii::t('common', '分店编号'),
            'user_id' => Yii::t('common', '操作员'),
            'created_at' => Yii::t('common', '创建时间'),
            'pay_cash' => Yii::t('common', '现金支付'),
            'pay_pos' => Yii::t('common', 'pos支付'),
            'pay_card' => Yii::t('common', '卡支付'),
            'pay_scholarship' => Yii::t('common', '奖学金支付'),
            'deposit' => Yii::t('common', '保证金'),
            'free' => Yii::t('common', '赠送金额'),
            'order_id' => Yii::t('common', '关联id'),
            'scholarship_rel_id' => Yii::t('common', '奖学金关联id'),
            'note' => Yii::t('common', '备注'),
            'state' => Yii::t('common','类型(0 其他 1充值 2抢奖学金 3转奖学金 4 保证金 5 订单 6卡奖学金 7退课 8会费)'),
            'pay_wxpay' => Yii::t('common','微信支付'),
            'pay_alipay' => Yii::t('common','支付宝支付'),
            'pay_password' => Yii::t('common','支付密码'),
            'full_activities_id' => Yii::t('common','充值方式'),
            'p_cash' => Yii::t('common','专属金额'),
            'money' => Yii::t('common','现金支付'),
            'student_id' => Yii::t('common','学生id'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['state'] = function (){
          switch ($this->state){
              case 1:
                  return \Yii::t('common','充值');
              case 2:
                  return \Yii::t('common','抢奖学金');
              case 3:
                  return \Yii::t('common','转奖学金');
              case 4:
                  return \Yii::t('common','保证金');
              case 5:
                  return \Yii::t('common','订单');
              case 6:
                  return \Yii::t('common','卡奖学金');
              case 7:
                  return \Yii::t('common','退课');
              case 8:
                  return \Yii::t('common','会费');
              default :
                  return \Yii::t('common','其他');

          }
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'update' => function ($data) {
                            return '';
                        },
                    ]
                )->create();
            },
            'customer_name' => function () {
                return ($this->customer != null) ? $this->customer->name : '';
            },
            'admin_name' => function () {
                return ($this->admin != null) ? $this->admin->username : '';
            },
            'account' => function () {
                return ($this->customer->account) ? $this->customer->account : '';
            },
            'full_activities_name' =>function(){
                return isset($this->fullactivities->name) ? $this->fullactivities->name : '';
            }

        ];
    }
    //验证支付订单时的总金额
    function validate_cash($attribute){
        if((int)$this->cash > 0){
            if(((int)$this->pay_wxpay + (int)$this->pay_alipay + (int)$this->pay_card + (int)$this->pay_cash) < (int)$this->cash){
                $this->addError($attribute, Yii::t('common', '输入的金额大于等于需付款的额度！'));
            }
        }

    }
    //验证实物支付订单时的总金额
    function validate_money($attribute){
        if((int)$this->money > 0){
            if(((int)$this->pay_wxpay + (int)$this->pay_alipay + (int)$this->pay_card + (int)$this->pay_cash) < (int)$this->money){
                $this->addError($attribute, Yii::t('common', '输入的金额大于等于需付款的额度！'));
            }
        }

    }
    //验证支付订单时的总金额
    function validate_password($attribute){
            $branch = CustomerBranchaccount::find()->where(['site_id'=>Fun::getSiteId(),'customer_id'=>$this->customer_id])->one();
            if($branch->pay_password != md5($this->$attribute)){
                $this->addError($attribute, Yii::t('common', '支付密码不正确！'));
            }


    }

    public function charge_required($attribute){
        $free = (int)$this->$attribute;
           if((((int)$this->pay_cash) + ((int)$this->pay_pos) + ((int)$this->pay_wxpay) + ((int)$this->pay_alipay)+ ((int)$this->pay_card) + $free) <= 0){
               $this->addError($attribute, Yii::t('common', '充值金额大于0'));
           }
    }

    //验证活动
    public function charge_activities($attribute){
        if($this->full_activities_id > 0){
            $full = FullActivities::findOne($this->full_activities_id);
            if(((int)$this->pay_cash) + ((int)$this->pay_pos) + ((int)$this->pay_wxpay) + ((int)$this->pay_alipay) + ((int)$this->pay_card) < $full->money || $this->free < $full->give_money){
                $this->addError($attribute, Yii::t('common', '实付、赠送金额要与活动一致！'));
            }
        }
    }

    public function actual_back_check($attribute)
    {

         $money = ($this->pay_cash) + ($this->pay_pos);
        if ($this->customer->account <$money) {
            $this->addError($attribute, Yii::t('common', '账户余额不足'));
        }
    }

    public function cash_check($attribute){
        if($this->pay_cash >10000){
            $this->addError($attribute, Yii::t('common', '现金支付金额不能大于10000'));
        }
    }

    public function pos_check($attribute){
        if($this->pay_pos >10000){
            $this->addError($attribute, Yii::t('common', 'POS机支付金额不能大于10000'));
        }
    }

    public function free_check($attribute){
        if($this->free >10000){
            $this->addError($attribute, Yii::t('common', '赠送金额不能大于10000'));
        }
    }

    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }

    public function getadmin()
    {
        return $this->hasOne(\zhtfrontend\models\datas\Admin::className(), ['id' => 'user_id']);
    }

    public function getFullactivities(){
        return $this->hasOne(FullActivities::className(),['id'=>'full_activities_id']);
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);

    }
}
